//
//  BNDOfficeDocHelper.m
//  OfficeDoc
//
//  Created by pie on 2022/4/1.
//  Copyright © 2022 pie. All rights reserved.
//

#import "BNDOfficeDocHelper.h"
#import "BNDOfficeDocListCell.h"
#import "BNDChooseCompanyHelper.h"
#import "BNDOfficeDocModel.h"
#import "BNDPushVCHandler.h"
#import "LVRefreshFooter.h"
#import "LVRefreshHeader.h"

@interface BNDOfficeDocHelper()<UITableViewDelegate,UITableViewDataSource,TableViewHelperDelegate>

@end

@implementation BNDOfficeDocHelper

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (instancetype)initWith:(UITableView *)tableView
{
    if((self = [super initWith:tableView]) != nil) {
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.delegate = self;
        self.keyWord = @"";
        @weakify(self)
        self.tableView.mj_footer = [LVRefreshFooter footerWithRefreshingBlock:^{
            @strongify(self)
            [self loadMore];
        }];
        
        self.tableView.mj_header = [LVRefreshHeader headerWithRefreshingBlock:^{
            @strongify(self)
            [self reload];
        }];
        
        [[[NSNotificationCenter defaultCenter] rac_addObserverForName:kNotificationRefreshSuccess object:nil] subscribeNext:^(id x) {
            @strongify(self)
            [self reload];
        }];
        
    }
    return self;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.datas.count;
}

- (BNDOfficeDocListCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *Identifier = @"BNDOfficeDocListCell";
    BNDOfficeDocListCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];
    if (!cell) {
        cell = [[BNDOfficeDocListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifier];
    }
    cell.model = self.datas[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    __block BNDOfficeDocModel *model = self.datas[indexPath.row];
    @weakify(self)
    [BNDPushVCHandler pushOfficeDocDetailsVc:model action:^{
        @strongify(self)
        model.unread = YES;
        [self.tableView reloadData];
    }];
}

- (CGFloat)tableView:(nonnull UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    return 142;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [[BNDPushVCHandler topViewController].view endEditing:YES];
}

- (void)setClassifyId:(NSString *)classifyId
{
    _classifyId = classifyId;
    [self reload];
}

#pragma mark -网络数据请求入口

- (void)load:(int)page tableView:(UITableView *)tableView
{
    @weakify(self)
    if(self.keyWord.length !=0) {
        [BNDApiManager officeDocRequestSearchrr:page content:self.keyWord success:^(NSArray<BNDOfficeDocModel *> * _Nonnull moreClass) {
            @strongify(self)
            [moreClass enumerateObjectsUsingBlock:^(BNDOfficeDocModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                @strongify(self)
                NSRange range = [TTStringNotNil(obj.title) rangeOfString:self.keyWord];
                NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc] initWithString:TTStringNotNil(obj.title)];
                [attribute addAttribute:NSFontAttributeName value:BNDPFHeavyFont(16) range:range];
                [attribute addAttribute:NSForegroundColorAttributeName value:RGB(55, 112, 235) range:range];
                obj.titleAttribute = attribute;
                
                NSMutableAttributedString *attribute1 = [[NSMutableAttributedString alloc] initWithString:obj.listContent];
                [attribute1 addAttribute:NSFontAttributeName value:BNDPFRegularFont(12) range:range];
                [attribute1 addAttribute:NSForegroundColorAttributeName value:RGB(55, 112, 235) range:range];
                obj.subtitleAttribute = attribute1;
            }];
            BND_BLOCK_EXEC(self.searchCountBlock,[@(moreClass.count) stringValue]);
            [self finish:moreClass errcode:0 inView:self.tableView];
        } failure:^(NSError * _Nullable error) {
            @strongify(self)
            [self finish:nil errcode:1 inView:self.tableView];
        }];
    }else{
        
        [BNDApiManager officeDocAnnouncementList:page classID:self.classifyId success:^(NSArray<BNDOfficeDocModel *> * _Nonnull moreClass) {
            @strongify(self)
            [moreClass enumerateObjectsUsingBlock:^(BNDOfficeDocModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                @strongify(self)
                NSRange range = [TTStringNotNil(obj.title) rangeOfString:self.keyWord];
                NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc] initWithString:TTStringNotNil(obj.title)];
                [attribute addAttribute:NSFontAttributeName value:BNDPFHeavyFont(16) range:range];
                [attribute addAttribute:NSForegroundColorAttributeName value:RGB(55, 112, 235) range:range];
                obj.titleAttribute = attribute;
                
                NSMutableAttributedString *attribute1 = [[NSMutableAttributedString alloc] initWithString:obj.listContent];
                [attribute1 addAttribute:NSFontAttributeName value:BNDPFRegularFont(12) range:range];
                [attribute1 addAttribute:NSForegroundColorAttributeName value:RGB(55, 112, 235) range:range];
                obj.subtitleAttribute = attribute1;
            }];
            [self finish:moreClass errcode:0 inView:self.tableView];
        } failure:^(NSError * _Nullable error) {
            @strongify(self)
            [self finish:nil errcode:1 inView:self.tableView];
        }];
    }
    
}

//- (void)setKeyWord:(NSString *)keyWord
//{
//    _keyWord = keyWord;
//    [self reload];
//}
@end
